package drrr.controller;

import drrr.orm.UserEntity;
import org.apache.struts2.ServletActionContext;

import java.util.Map;

/**
 * Created by Two_Cong on 15/05/27.
 */
public class ChagUserProfileAction extends BaseAction {
    private String username;
    private String password;//以前的密码
    private String email;
    private String newPassword;//新密码


    public  String  getUsername(){
        return username;
    }
    public  void setUsername(String username){this.username=username;}
    public  String getPassword(){
        return password;
    }
    public void setPassword(String password){
        this.password=password;
    }
    public  String getEmail(){
         return email;
    }
    public void setEmail(String email){this.email=email;}
    public String getNewPassword(){
        return newPassword;
    }
    public void setNewPassword(String newPassword){
        this.newPassword=newPassword;
    }

    public String execute() throws Exception {

        Map<String,Object> session= ServletActionContext.getContext().getSession();
       //userEntity1获取user中的当前用户的信息:username password email
        UserEntity userEntity1= ((UserEntity) session.get("user"));
        //当前密码是否与数据库中密码匹配，

        if (this.getPassword().equals(userEntity1.getPassword()) ){
        //匹配成功，更新密码用户名以及email
            UserEntity userEntity=new UserEntity();
            userEntity.setUsername(this.getUsername());
            userEntity.setPassword(this.getNewPassword());
            userEntity.setEmail(this.getEmail());

            userEntity.setId(userEntity1.getId());

            getAllService().getUserService().update(userEntity);
            session.put("user",userEntity);
            return  SUCCESS;

        }
        else
            return ERROR;
    }

}
